package cn.fxl.basic.controller;

import cn.fxl.basic.result.AjaxResult;
import cn.fxl.basic.utils.FastDfsUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    @PostMapping
    public AjaxResult upload(@RequestPart(value = "file") MultipartFile file){
        try {
//            xxx.msg
            String originalFilename = file.getOriginalFilename();
           String msg= originalFilename.substring(originalFilename.lastIndexOf(".")+1);//msg
            String filePath = FastDfsUtils.upload(file.getBytes(), msg);
          return  AjaxResult.me().setResultObj(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"上传失败");
        }
    }
    @DeleteMapping
    public AjaxResult del(@RequestParam("path") String path){
        try {
            //       path          /group1/M00/00/0F/oYYBAGJ6JmyATaYbAAk1mWiyf0E903.gif
//        substring    group1/M00/00/0F/oYYBAGJ6JmyATaYbAAk1mWiyf0E903.gif
            String s = path.substring(1);
//            group1
            String groupName = s.substring(0, s.indexOf("/"));
            String remotePath = s.substring(s.indexOf("/") + 1);
            FastDfsUtils.delete(groupName,remotePath);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删败");
        }

    }
}
